package juego.main;

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;



import juego.control.PersonajeGrafico;
import juego.entidades.Entidad;
import juego.graficos.ElementoGrafico;
import juego.niveles.Nivel;
import juego.niveles.generator.CreatorByFile;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Juego extends javax.swing.JFrame {
	private JScrollPane jScrollPane;
	private JPanel jPanel1;
	private JLabel jLabel1;
    private PersonajeGrafico kirby;
    private boolean control=false;
	private boolean controlUp=false;
	private Nivel level;
	private int x=13;
	private int y=0;
	private int up=0;
	private int mov=0;
	private int valor=0;

	

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Juego inst = new Juego();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
				inst.setBounds(0, 100, 1100, 600);
				
			}
		});
		
		
	}
	
	public Juego() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jScrollPane = new JScrollPane();
				getContentPane().add(jScrollPane, BorderLayout.CENTER);
				jScrollPane.setBackground(new java.awt.Color(0,100,100));
				
				jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
				jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
				jScrollPane.setOpaque(false);
				
				
				{
					jPanel1 = new JPanel();
					jScrollPane.setViewportView(jPanel1);
					jScrollPane.setBackground(Color.YELLOW);
					
					jPanel1.setLayout(null);
		
					this.addKeyListener(new KeyAdapter() {
						public void keyReleased(KeyEvent evt) {
							thisKeyReleased(evt);
						}
						public void keyPressed(KeyEvent evt) {
							thisKeyPressed(evt);
						}
					});
								
					
					jPanel1.setBackground(Color.YELLOW);
					jPanel1.setSize(2000,448);
					jPanel1.setPreferredSize(new Dimension(3200,500));
					jPanel1.setBounds(0, 0, 9600, 500);

				
					jScrollPane.setViewportView(jPanel1);
					
									
				}
				
			}
			
			
			 
			
			kirby=new PersonajeGrafico(level);
			jPanel1.add(kirby);
			kirby.setBounds(0, 380, 49, 50);
		
			pack();
			setSize(400, 300);
			loadLevel();
		} catch (Exception e) {	e.printStackTrace();}
	}
	
	private void loadLevel()
	{
		File archivo= new File ("./levels/level1.txt");
		CreatorByFile cbf= new CreatorByFile(archivo);
		level = cbf.getLevel();
		loadGraphicLevel( level );
	}
	
	private void loadGraphicLevel( Nivel level )
	{
		for( int i = 0; i < 17; i++ )
		{
			for( int j = 0; j < 100; j++ )
			{
				Entidad e = level.getEntidad(i, j);
				if (e!=null){
				ElementoGrafico eg = e.getGrafico();
				eg.setLocation(j*32, i*32);
				this.jPanel1.add(eg);
				}
			}
		}
	
	}
	
private void thisKeyPressed(KeyEvent evt) {
	
if (evt.getKeyCode() == KeyEvent.VK_RIGHT){
	if (!controlUp){
		control=true;
		kirby.derecha();
		if ( level.getEntidad(x, y)!=null){
			if(!level.getEntidad(x,y).esTransitable()){
				if (level.getEntidad(x-1, y+1)!=null){
					if (level.getEntidad(x-1, y+1).esTransitable()){
					
					kirby.derecha();
					y ++;
					kirby.setLocation(kirby.getLocation().x+32 ,kirby.getLocation().y);
					jScrollPane.getHorizontalScrollBar().setValue(valor+=25);
					caerGravedad();
					} else {kirby.derecha();}
					}}
				else {
				caerGravedad();
				
				}
				
				}
			
		} else {
			if (level.getEntidad(x-1,y).esTransitable()){
				up+=32;
				x-=1;
				kirby.setLocation(kirby.getLocation().x,kirby.getLocation().y-32);
				if (level.getEntidad(x,y+1).esTransitable()){
					
					y+=1;
					
					kirby.setLocation(kirby.getLocation().x+32,kirby.getLocation().y);
					
					jScrollPane.getHorizontalScrollBar().setValue(valor+=25);
					if (level.getEntidad(x-1,y).esTransitable()){
						x-=1;
						up+=32;
						kirby.setLocation(kirby.getLocation().x,kirby.getLocation().y-32);
						
						if (level.getEntidad(x,y+1).esTransitable()){
							y+=1;
							
							kirby.setLocation(kirby.getLocation().x+32,kirby.getLocation().y);
							if (y>15){
							jScrollPane.getHorizontalScrollBar().setValue(valor+=25);}
							if (!level.getEntidad(x+1,y).esTransitable()){  
								  kirby.nada();
							}
							else {
								caerGravedad(); 
								jScrollPane.getHorizontalScrollBar().setValue(valor+=32);
						  }
					  
					  caerGravedad(); 
					 
					  }
				  
				  caerGravedad();
				  
				  }
				caerGravedad(); 
				
					}
			caerGravedad();
			
					
			}
				
				
		}
		
		
	if (y==99){
			Icon icono = new ImageIcon(getClass().getClassLoader().getResource("juego/graficos/win.jpg"));
			JOptionPane.showMessageDialog(null,  "","KIRBY", JOptionPane.OK_OPTION, icono);
			this.dispose();
		
		}
	}


if (evt.getKeyCode() == KeyEvent.VK_LEFT){
		if (!controlUp){	
			if ( level.getEntidad(x, y)!=null){
				control=true;
				if(!level.getEntidad(x,y).esTransitable()){
							
					if (y>0 ){
					if (level.getEntidad(x-1, y-1)!=null){
					if (level.getEntidad(x-1, y-1).esTransitable()){
					kirby.izquierda();
					y --;
					if (y<77){
					jScrollPane.getHorizontalScrollBar().setValue(valor-=25);
					}
					
					kirby.setLocation(kirby.getLocation().x-32 ,kirby.getLocation().y);
					
					
					jScrollPane.getHorizontalScrollBar().setValue(valor-=15);
					caerGravedad();
					}
							else {kirby.izquierda();}}
					}else {
						kirby.izquierda();
						}
					} else {
						caerGravedad();
					
					}
				
				}}
					else if (y>1 ){
						if (level.getEntidad(x-1,y).esTransitable()){
							up+=32;
							x-=1;
							kirby.setLocation(kirby.getLocation().x,kirby.getLocation().y-32);
							if (level.getEntidad(x,y-1).esTransitable()){
								y-=1;
								if (y<30){
								jScrollPane.getHorizontalScrollBar().setValue(valor-=40);}
								kirby.setLocation(kirby.getLocation().x-32,kirby.getLocation().y);
								
								if (level.getEntidad(x-1,y).esTransitable()){
									x-=1;
									up+=32;
									kirby.setLocation(kirby.getLocation().x,kirby.getLocation().y-32);
									if (level.getEntidad(x,y-1).esTransitable()){
										y-=1;
										
										if (y>40 && y<70){
										jScrollPane.getHorizontalScrollBar().setValue(valor-=40);}
										kirby.setLocation(kirby.getLocation().x-32,kirby.getLocation().y);
										
										if (!level.getEntidad(x+1,y).esTransitable()){  
												kirby.nada();
											}
											else {
												
													caerGravedad(); 
													jScrollPane.getHorizontalScrollBar().setValue(valor-=15);
													}
											caerGravedad();
											jScrollPane.getHorizontalScrollBar().setValue(valor-=15);
										}
				  					caerGravedad(); 
				  					jScrollPane.getHorizontalScrollBar().setValue(valor-=15);
									}
						  caerGravedad(); 
						  jScrollPane.getHorizontalScrollBar().setValue(valor-=15);
							}
				 	  caerGravedad();
				 	 jScrollPane.getHorizontalScrollBar().setValue(valor-=32);
					}
				
				
			}
		
			
		
		
		
		
		
		}
		
if (evt.getKeyCode() == KeyEvent.VK_UP){
	if ( !controlUp){
		control=true;
		controlUp=true;
		kirby.arriba();
		if ( level.getEntidad(x-1, y)!=null){
			if(level.getEntidad(x-1,y).esTransitable()){
				kirby.arriba();
				kirby.setLocation(kirby.getLocation().x ,kirby.getLocation().y-32);
				up+=32;
				x--;
				}
				else {
					kirby.arriba();
				}
				
		}} 
		
		}
		
		
if (evt.getKeyCode() == KeyEvent.VK_DOWN){
		if (!control){
		 	kirby.agachado();
	        control=true;
		}
}
		

}

private void caerGravedad(){
	if(level.getEntidad(x,y)!=null){
		while (up>=0 && level.getEntidad(x,y).esTransitable()){
		kirby.setLocation(kirby.getLocation().x ,kirby.getLocation().y+32);
		up-=32;
		x++;
		}
		if (up>0 && !level.getEntidad(x,y).esTransitable()){ 
			kirby.nada();}
		
		if (x==14 ){ 
		    kirby.caer();
		    kirby.setLocation(kirby.getLocation().x ,kirby.getLocation().y+32);
			kirby.setLocation(kirby.getLocation().x ,kirby.getLocation().y+32);
			kirby.setLocation(kirby.getLocation().x ,kirby.getLocation().y+32);
			
			Icon icono = new ImageIcon(getClass().getClassLoader().getResource("juego/graficos/lost.jpg"));
			JOptionPane.showMessageDialog(null,  "","KIRBY", JOptionPane.OK_OPTION, icono);
			this.dispose();
			
				 
	}}}



private void thisKeyReleased (KeyEvent evt){
	
if (evt.getKeyCode() == KeyEvent.VK_RIGHT){
		kirby.nada();
		control=false;
		controlUp=false;
		}
	
if (evt.getKeyCode() == KeyEvent.VK_LEFT){
		kirby.nada();
		control=false;
		controlUp=false;
		}
	
if (evt.getKeyCode() == KeyEvent.VK_UP){
		kirby.nada();
		controlUp=false;
		control=false;
		caerGravedad();
	}
	
	
if (evt.getKeyCode() == KeyEvent.VK_DOWN){
		kirby.nada();
		control=false;
		}
}



}